<template>
  <div class="app-container">
    <!-- <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form> -->

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['sf:createOrder:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['sf:createOrder:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['sf:createOrder:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['sf:createOrder:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="createOrderList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键id" align="center" prop="id" /> -->
      <el-table-column label="业务订单号" align="center" prop="merchantOrderNo" width="160"/>
      <!-- <el-table-column label="快件产品编码" align="center" prop="expressType" /> -->
      <el-table-column label="付款方式" align="center" prop="payMethod">
        <template slot-scope="scope">
          <template v-if="scope.row.payMethod==1">
              {{ scope.row.monthlyCard?'寄付月结':'寄付现结'}}
            </template>
          <dict-tag :options="dict.type.logistics_payment_types" :value="scope.row.payMethod" v-else/> 
        </template>
      </el-table-column>
      <el-table-column label="月结卡号" align="center" prop="monthlyCard" width="120"/> 
      <el-table-column label="包裹数" align="center" prop="packagesNo" /> 
      <el-table-column label="保价类型" align="center" prop="insureType" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.logistics_insurance_types" :value="scope.row.insureType"/> 
        </template>
      </el-table-column>
      <el-table-column label="寄托物类型" align="center" prop="productCode" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.logistics_product_type" :value="scope.row.productCode"/> 
        </template> 
      </el-table-column>
      <el-table-column label="声明价值" align="center" prop="statementValue" >
        <template slot-scope="scope">
          ￥{{(scope.row.statementValue/100).toFixed(2)}}
        </template>
      </el-table-column>
      <el-table-column label="是否保鲜" align="center" prop="retainFreshness" width="130">
        <template slot-scope="scope">
          {{scope.row.retainFreshness=='true'?'是':'否'}}
        </template>
      </el-table-column>
      <el-table-column label="运单号" align="center" prop="mailNo" width="140"/>
      <el-table-column label="签回单号" align="center" prop="returnTrackingNo" width="140"/>
      <el-table-column label="取件时间" align="center" prop="sendStartTime" width="160"/>
      <el-table-column label="订单状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.logistics_order_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" show-overflow-tooltip />
      <el-table-column label="创建时间" align="center" prop="createTime" width="160"/> 
      <el-table-column label="附加数据" align="center" prop="attach" show-overflow-tooltip/>
      <el-table-column label="来源" align="center" prop="sourceId" /> 
      <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleEditTable(scope.row)"
            v-hasPermi="['sf:createOrder:edit']"
            >详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改顺丰快递-创建订单信息[创建订单]对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="客户订单号" prop="merchantOrderNo">
          <el-input v-model="form.merchantOrderNo" placeholder="请输入客户订单号" />
        </el-form-item>
        <el-form-item label="运费付款方式" prop="payMethod">
          <el-input v-model="form.payMethod" placeholder="请输入运费付款方式" />
        </el-form-item>
        <el-form-item label="顺丰月结卡号" prop="monthlyCard">
          <el-input v-model="form.monthlyCard" placeholder="请输入顺丰月结卡号" />
        </el-form-item>
        <el-form-item label="取件时间" prop="sendStartTime">
          <el-input v-model="form.sendStartTime" placeholder="请输入取件时间" />
        </el-form-item>
        <el-form-item label="声明价值" prop="statementValue">
          <el-input v-model="form.statementValue" placeholder="请输入声明价值" />
        </el-form-item>
        <el-form-item label="是否需要保鲜服务" prop="retainFreshness">
          <el-input v-model="form.retainFreshness" placeholder="请输入是否需要保鲜服务" />
        </el-form-item>
        <el-form-item label="包裹数" prop="packagesNo">
          <el-input v-model="form.packagesNo" placeholder="请输入包裹数" />
        </el-form-item>
        <el-form-item label="产品Code" prop="productCode">
          <el-input v-model="form.productCode" placeholder="请输入产品Code" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="附加数据" prop="attach">
          <el-input v-model="form.attach" placeholder="请输入附加数据" />
        </el-form-item>
        <el-form-item label="顺丰返回运单号" prop="mailNo">
          <el-input v-model="form.mailNo" placeholder="请输入顺丰返回运单号" />
        </el-form-item>
        <el-form-item label="顺丰返回签回单号" prop="returnTrackingNo">
          <el-input v-model="form.returnTrackingNo" placeholder="请输入顺丰返回签回单号" />
        </el-form-item>
        <el-form-item label="来源" prop="sourceId">
          <el-input v-model="form.sourceId" placeholder="请输入来源" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.logistics_order_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCreateOrder, getCreateOrder, delCreateOrder, addCreateOrder, updateCreateOrder } from "@/api/sf/createOrder";

export default {
  name: "CreateOrder",
  dicts: ['logistics_order_status','logistics_payment_types','logistics_product_type','logistics_insurance_types'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 顺丰快递-创建订单信息[创建订单]表格数据
      createOrderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        merchantOrderNo: [
          { required: true, message: "客户订单号不能为空", trigger: "blur" }
        ],
        expressType: [
          { required: true, message: "快件产品编码不能为空", trigger: "change" }
        ],
        payMethod: [
          { required: true, message: "运费付款方式不能为空", trigger: "blur" }
        ],
        packagesNo: [
          { required: true, message: "包裹数不能为空", trigger: "blur" }
        ],
        productCode: [
          { required: true, message: "产品Code不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询顺丰快递-创建订单信息[创建订单]列表 */
    getList() {
      this.loading = true;
      listCreateOrder(this.queryParams).then(response => {
        this.createOrderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        merchantOrderNo: null,
        expressType: null,
        payMethod: null,
        monthlyCard: null,
        callFlag: null,
        sendStartTime: null,
        statementValue: null,
        insureType: null,
        retainFreshness: null,
        packagesNo: null,
        productCode: null,
        needHandover: null,
        remark: null,
        attach: null,
        createTime: null,
        mailNo: null,
        returnTrackingNo: null,
        sourceId: null,
        status: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加顺丰快递-创建订单信息[创建订单]";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCreateOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改顺丰快递-创建订单信息[创建订单]";
      });
    },
     /** 修改按钮操作 */
     handleEditTable(row) {
      const id = row.id;
      const params = { pageNum: this.queryParams.pageNum };
      this.$tab.openPage("详情", "/sfcreateOrder/detail/index/" + id, params);
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCreateOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCreateOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除顺丰快递-创建订单信息[创建订单]编号为"' + ids + '"的数据项？').then(function() {
        return delCreateOrder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('sf/createOrder/export', {
        ...this.queryParams
      }, `createOrder_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
